<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ include file="../config/config.jsp"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@page
	import="java.util.*,com.cinema.Bean.*,com.cinema.dao.*,com.cinema.dao.imp.*"%>

<%
	IMovieSessionDao  iMovieSessionDao=new MovieSessionDaoMysql();
	List<MovieSession> movieSession = null;
	if(session.getAttribute("slist")==null){
		movieSession=iMovieSessionDao.selectAll();
		session.setAttribute("slist",movieSession);
	}else{
		movieSession=(List<MovieSession>)session.getAttribute("slist");
	}
	
	 //定义变量，实现分页
	int count=10;//设置每页显示的数据条数
	int size=movieSession.size();//记录总的条数
	int pageNumber=(int)Math.ceil(size*1.0/count);//可分页数
	
	int thisPage=0;//当前页数
	
	//减少数据库的加载
	if(request.getParameter("thisPage")!=null){
		thisPage=Integer.parseInt(request.getParameter("thisPage"));
	}
	
	//越界处理
	if(thisPage<0){
		thisPage=0;
	}else if(thisPage>pageNumber){
		thisPage=pageNumber;
	} 
	
 %>

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>场次首页</title>
<link rel="stylesheet" href="<%=basePath %>/css/bootstrap.min.css" />
<link rel="stylesheet" href="<%=basePath %>/css/bootstrap-theme.min.css" />
<style type="text/css">
.sidebar-nav {
	padding: 9px 0;
}

@media ( max-width : 980px) {
	/* Enable use of floated navbar text */
	.navbar-text.pull-right {
		float: none;
		padding-left: 5px;
		padding-right: 5px;
	}
}
</style>
</head>
<body>
	<form class="form-inline definewidth m20" action="index.jsp"
		method="get" style="padding: 10px 0 15px 10px;">
		输入关键字 : <input type="text" name="keyWord" id="keyWord"
			class="form-control" placeholder="请输入场厅id或电影id" value="">&nbsp;&nbsp;
		<button type="submit" class="btn btn-primary select">查询</button>
		&nbsp;&nbsp;
		<a type="button" href="<%=basePath %>/infoServlet?method=getMovieInfo" class="btn btn-success">新增场次</a>
	</form>

	<table class="table table-bordered table-hover" id="table"
		style="background: #EDEDED;">
		<thead>
			<tr>
				<th class="text-center">编号</th>
				<th class="text-center">场厅</th>
				<th class="text-center">电影</th>
				<th class="text-center">时间</th>
				<th class="text-center">票价</th>
				<th class="text-center">操作</th>
			</tr>
		</thead>
		<%
	    	for(int i=thisPage*count;i<(thisPage+1)*count && i<movieSession.size();i++){
	    		MovieSession a=movieSession.get(i);
	     %>

		<tr>
			<td class="text-center"><%=a.getSessionId()%></td>
			<td class="text-center"><%=a.getHallId() %></td>
			<td class="text-center"><%=a.getMovieId() %></td>
			<td class="text-center"><%=a.getTime() %></td>
			<td class="text-center"><%=a.getPrice()%></td>

			<td class="text-center"><a
				href="../movieSessionServlet?method=getMovieSession&sessionId=<%=a.getSessionId() %>">修改</a>&nbsp;&nbsp;
				<a href="function:void(0);" class="delete">删除</a></td>
		</tr>
		<%
	     	}
	      %>
	</table>

	<div class="inline  page" style="text-align: left; margin-left: 35%;">
		共&nbsp;<%=size %>&nbsp;条记录,当前显示第&nbsp;<%=thisPage+1 %>&nbsp;页 <a
			href="index.jsp?thisPage=0">首页</a> <a
			href="index.jsp?thisPage=<%=thisPage-1%>">上一页</a>
		<%
            	for(int i=0;i<pageNumber;i++){
             %>
		<a href="index.jsp?thisPage=<%=i%>"><%=i+1 %></a>
		<%
             	}
              %>
		<a href="index.jsp?thisPage=<%=thisPage+1%>">下一页</a> <a
			href="index.jsp?thisPage=<%=pageNumber-1 %>">尾页</a>
	</div>
</body>
<script src="<%=basePath %>/js/jquery-3.2.1.min.js"></script>
<script src="<%=basePath %>/js/bootstrap.min.js"></script>
<script type="text/javascript">
    $(function () {
    	$(".select").click(function() {
			var key = $("#keyWord").val();//得到输入框的值即搜索的关键字
			var url = "../movieSessionServlet";//将请求发送到这个地址
			var args = {
				"method" : "select",
				"keyword" : key,
				"time" : new Date()
			};
			$.post(url, args, function(data) {
				window.location.href = "index.jsp";
			});
		});
    	
		$(".delete").click(function(){
		    var $tr=$(this).parent().parent();
		    var $td=$tr.find("td").first();
		    var id=$td.text();
		    
		    if(confirm("确定要删除吗？")){
			    
			    var url="../movieSessionServlet";
			    var args={"sessionId":id,"method":"delete","time":new Date()};
			    $.post(url,args,function(data){
			      if("success"==data){
			        alert('删除成功！');
			      }else if("error"==data){
			        alert('删除失败！'); 
			      }
					window.location.href="index.jsp";	
			    });
		      }
		    return false;//取消默认行为
		 });
    });
</script>
</html>